package com.carhome.adapter;

import java.util.ArrayList;
import java.util.List;


import com.carhome.R;
import com.carhome.bean.ArticleNewsInfo;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ArticleFragment1ListAdapter extends BaseAdapter {
	BitmapUtils mBitmapUtils;

	List<ArticleNewsInfo> list = new ArrayList<ArticleNewsInfo>();
	private Context context;

	public ArticleFragment1ListAdapter(Context context) {
		this.context = context;
		mBitmapUtils = new BitmapUtils(context);
		mBitmapUtils.configDefaultLoadingImage(R.drawable.autohome_loadingpage_bg);
		mBitmapUtils.configDefaultLoadFailedImage(R.drawable.autohome_loadingpage_bg);
	}

	public void setData(List<ArticleNewsInfo> listDatas) {
		if (listDatas == null) {
			return;
		}
		list.addAll(listDatas);
	}

	public int getCount() {
		return list.size();
	}

	@Override
	public ArticleNewsInfo getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.article_fragment_listview_item, null);

			holder.fosuingNews = (LinearLayout) convertView
					.findViewById(R.id.article_fragment_listview_focusingNews);
			holder.focusingImg1 = (ImageView) convertView
					.findViewById(R.id.article_fragment_listview_focusingNews_img1);
			holder.focusingImg2 = (ImageView) convertView
					.findViewById(R.id.article_fragment_listview_focusingNews_img2);
			holder.focusingImg3 = (ImageView) convertView
					.findViewById(R.id.article_fragment_listview_focusingNews_img3);
			holder.focusingNewsTime = (TextView) convertView
					.findViewById(R.id.article_fragment_listview_focusingNews_time);
			holder.focusingNewsTitle = (TextView) convertView
					.findViewById(R.id.article_fragment_listview_focusingTitle);

			holder.nomalNews = (RelativeLayout) convertView
					.findViewById(R.id.article_fragment_listview_nomalNews);
			holder.newsImg = (ImageView) convertView
					.findViewById(R.id.article_fragment_listview_item_newsImg);
			holder.newsTime = (TextView) convertView
					.findViewById(R.id.article_fragment_listview_newsTime);
			holder.newsTitle = (TextView) convertView
					.findViewById(R.id.article_fragment_listview_newsTitle);
			convertView.setTag(holder);
		}

		holder = (ViewHolder) convertView.getTag();
		if (!"".equals(getItem(position).smallpic)) { // listview的item以常规方式显示
			holder.nomalNews.setVisibility(View.VISIBLE);
			holder.fosuingNews.setVisibility(View.GONE);

			mBitmapUtils.display(holder.newsImg, getItem(position).smallpic);
			holder.newsTime.setText(getItem(position).time);
			holder.newsTitle.setText(getItem(position).title);
		} else {
			String urlStrings = getItem(position).indexdetail;
			String[] spliturls = new String[3];

			int count = 0;
			String[] words = urlStrings.split("(,)|(http)");
			for (int i = 0; i < words.length; i++) {
				words[i] = "http" + words[i];
				if (words[i].contains("http://")) {
					spliturls[count++] = words[i];
				}
			}

			holder.nomalNews.setVisibility(View.GONE);
			holder.fosuingNews.setVisibility(View.VISIBLE);
			mBitmapUtils.display(holder.focusingImg1, spliturls[0]);
			mBitmapUtils.display(holder.focusingImg2, spliturls[1]);
			mBitmapUtils.display(holder.focusingImg3, spliturls[2]);
			holder.focusingNewsTime.setText(getItem(position).time);
			holder.focusingNewsTitle.setText(getItem(position).title);
		}

		return convertView;
	}

	private class ViewHolder {
		RelativeLayout nomalNews;
		TextView newsTime;
		TextView newsTitle;
		ImageView newsImg;

		LinearLayout fosuingNews;
		TextView focusingNewsTime;
		TextView focusingNewsTitle;
		ImageView focusingImg1;
		ImageView focusingImg2;
		ImageView focusingImg3;
	}

	public void removeAll() {
		list.clear();
	}

}
